﻿//根据cookie名称取cookie
function getCookie(cookieName) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(cookieName + "=")
        if (c_start != -1) {
            c_start = c_start + cookieName.length + 1;
            c_end = document.cookie.indexOf("^", c_start);
            if (c_end == -1)
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

//设置cookie
function setCookie(c_userName, c_userName_value, c_password, c_password_value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_userName + "=" + escape(c_userName_value) + "^" + c_password + "=" + escape(c_password_value) + ((expiredays == null) ? "" : "^;expires=" + exdate.toGMTString());
    //alert(getCookie(c_userName));
    //console.log(document.cookie)
}

//检测cookie是否存在，存在，自动登录
function checkCookie(c_userName, c_password) {
    //alert(document.cookie)
    var username = getCookie(c_userName);
    var password = getCookie(c_password);
    if (username != null && username != "" && password != null && password != "") { //如果
        $('#txtUserName').val(username);
        $('#txtPassword').val(password)
        $("#chkRemember").attr("checked", "false");
    }
}

//使cookie过期
function cleanCookie(c_userName,c_password) {
    var username = getCookie(c_userName);
    var password = getCookie(c_password);
    if (username != null && username != "" && password != null && password != "") {
        document.cookie = c_userName + "=" + ";" + c_password + "=" + ";expires=Thu, 01-Jan-70 00:00:01 GMT";
    }  
}